#include "LCOMProcessor.h"
#include <mastring.h>
#include <mastdlib.h>
#include <matime.h>
#include "conprint.h"
#include "../std.h"
#include "../Utilities/masscanf.h"
#include "../Utilities/trim.h"
#include "../Utilities/SoapHelper.h"

LCOMProcessor::LCOMProcessor(){

}

LCOMProcessor::~LCOMProcessor(){
	APPCONTROLLER->getConnection()->close();
}

//XmlListener
void LCOMProcessor::mtxEncoding(const char* value)
{

}
void LCOMProcessor::mtxTagStart(const char* name, int len){

	if (mFirstTag.length()==0){
		mFirstTag.append(name, len);
		mInBody = false;
		mInItem = false;
		return;
	}
	if (strncmp(name, "soap:Body", 9)==0){
		mInBody = true;
		//lprintfln("in body");
		return;
	}
	if (strncmp(name, "Provinces",9) == 0){
		mLcomTag == LCOM_NULL;
		mInItem = true;
		mCurrentItem.companyid.clear();
		mCurrentItem.companyname.clear();
		mCurrentItem.drawdate.clear();
		mCurrentItem.outofdate.clear();
		mCurrentItem.shortcode.clear();
		mCurrentItem.smscnt.clear();
		return;
	}else if (strncmp(name, "CompanyId", 9 ) ==0){
		mLcomTag = LCOM_ID;
		return;
	}else if (strncmp(name, "CompanyName", 11 ) ==0){
		mLcomTag = LCOM_NAME;
		return;
	}else if (strncmp(name, "DrawDate", 8 ) ==0){
		mLcomTag = LCOM_DRAWDATE;
		return;
	}else if (strncmp(name, "SmsCnt", 6 ) ==0){
		mLcomTag = LCOM_SMSCNT;
		return;
	}else if (strncmp(name, "ShortCode", 9 ) ==0){
		mLcomTag = LCOM_SHORTCODE;
		return;
	}else if (strncmp(name, "OutOfDate", 9 ) ==0){
		mLcomTag = LCOM_OUTOFDATE;
		return;
	}
}

void LCOMProcessor::mtxTagAttr(const char* attrName, const char* attrValue){

}
void LCOMProcessor::mtxTagStartEnd(){
	if (mFirstTag.length()== 0){
		mListener->processingComplete();
	}
}
void LCOMProcessor::mtxTagData(const char* data, int len){
	temp.append(data, len);
}
void LCOMProcessor::mtxTagEnd(const char* name, int len){
	//trim(temp);
	//parseControlCodes(temp);
	//stripHTML(temp);

	switch(mLcomTag){
	case LCOM_NULL:
		break;
	case LCOM_ID:
		mCurrentItem.companyid = temp;
		break;
	case LCOM_NAME:
		mCurrentItem.companyname = temp;
		break;
	case LCOM_DRAWDATE:
		mCurrentItem.drawdate = temp;
		break;
	case LCOM_SMSCNT:
		mCurrentItem.smscnt = temp;
		break;
	case LCOM_SHORTCODE:
		mCurrentItem.shortcode = temp;
		break;
	case LCOM_OUTOFDATE:
		mCurrentItem.outofdate = temp;
		break;
	}

	temp.clear();

	mLcomTag = LCOM_NULL;

	if (strncmp(name, "Provinces", 9)==0){
		mInItem = false;
		if (mListener != NULL){
			mListener->itemReceived(mCurrentItem);
		}
	}

	if(strcmp(name, "soap:Body") == 0) {
			mInBody = false;
		}

	if (mFirstTag == name){
		mFirstTag.clear();
		mListener->processingComplete();
	}
}

void LCOMProcessor::mtxParseError(){

}
void LCOMProcessor::mtxEmptyTagEnd(){

}
unsigned char LCOMProcessor::mtxUnicodeCharacter(int unicode){
	/*char c = XmlListener::mtxUnicodeCharacter(unicode);
	if (c == 0){
		c = '?';
	}
	return c;*/
	char c = getUnicodeCharacter(unicode);
	return c;
}

//Listener
void LCOMProcessor::setLCOMListener(LCOMListener* listener){
	mListener = listener;
}

void LCOMProcessor::addRequest(String& url, LCOMListener* requester){
	if (APPCONTROLLER->getConnection()->isBusy()) return;

	//Create Request
	String sSoap = SH_LotProvinces();
	int sSoapLen = sSoap.length();
	String sSoapLenStr = integerToString(sSoapLen);
	String sLength = "Content-Length";
	String sType = "Content-Type";
	String sTypeValue = "text/xml;charset=UTF-8";

	APPCONTROLLER->getConnection()->clearHeaders();
	APPCONTROLLER->getConnection()->setHeader(sLength, sSoapLenStr);
	APPCONTROLLER->getConnection()->setHeader(sType, sTypeValue);
	APPCONTROLLER->getConnection()->setData(sSoap);
	APPCONTROLLER->getConnection()->setXmlListener(this);

	//Start download
	APPCONTROLLER->getConnection()->setURL(url.c_str());
	mListener = requester;
	lprintfln("begin LCOM download");
	APPCONTROLLER->getConnection()->beginDownloading();
}


